package in.abip.chisame;

import in.abip.chisame.model.Camera;
import in.abip.chisame.model.Room;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

public class CameraFormActivity extends Activity {
	private Camera _model = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.camera_form);
		populate();
		bind();
	}

	private Camera loadModel() {
		if (_model == null) {
			try {
				Bundle bundle = getIntent().getExtras();
				Integer cameraId = bundle.getInt("cameraId");
				_model = (Camera) Camera.model().findByPk(cameraId);
			} catch (Exception e) {
				_model = new Camera();
			}
		}
		return _model;
	}

	protected void save() {

		Camera camera = loadModel();
		EditText editText;

		editText = (EditText) this.findViewById(R.id.name);
		camera.name = editText.getText().toString();

		editText = (EditText) this.findViewById(R.id.ipAddress);
		camera.ipAddress = editText.getText().toString();

		editText = (EditText) this.findViewById(R.id.channel);
		camera.channel = Integer.parseInt(editText.getText().toString());

		Spinner spinner = (Spinner) this.findViewById(R.id.location);
		Room room = (Room) spinner.getSelectedItem();
		camera.roomId = room._id;

		editText = (EditText) this.findViewById(R.id.username);
		camera.username = editText.getText().toString();

		editText = (EditText) this.findViewById(R.id.password);
		camera.password = editText.getText().toString();

		camera.save();
	}

	protected void populate() {
		ArrayAdapter<Object> adapter = new ArrayAdapter<Object>(this,
			android.R.layout.simple_spinner_item, Room.model().findAll());
		adapter.setDropDownViewResource(
			android.R.layout.simple_spinner_dropdown_item
		);
		Spinner spinner = (Spinner) this.findViewById(R.id.location);
		spinner.setAdapter(adapter);
		
		Camera camera = loadModel();

		if (camera._id == null) return;
		
		EditText editText;

		editText = (EditText) this.findViewById(R.id.name);
		editText.setText(camera.name);

		editText = (EditText) this.findViewById(R.id.ipAddress);
		editText.setText(camera.ipAddress);

		editText = (EditText) this.findViewById(R.id.channel);
		editText.setText(camera.channel.toString());

		Room room = null;
		for (int i = 0; i < adapter.getCount(); i++) {
			room = (Room) adapter.getItem(i);
			if (camera.roomId.equals(room._id)) {
				spinner.setSelection(i);
				break;
			}
		}

		editText = (EditText) this.findViewById(R.id.username);
		editText.setText(camera.username);

		editText = (EditText) this.findViewById(R.id.password);
		editText.setText(camera.password);
	}

	protected void bind() {
		OnClickListener okListener = new OnClickListener() {
			public void onClick(View view) {
				save();
				CameraFormActivity.this.finish();
			}
		};
		View view = (View) findViewById(R.id.ok);
		view.setOnClickListener(okListener);

		OnClickListener cancelListener = new OnClickListener() {
			public void onClick(View view) {
				CameraFormActivity.this.finish();
			}
		};
		view = (View) findViewById(R.id.cancel);
		view.setOnClickListener(cancelListener);
	}

}
